package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.entity.Pet;
import tech.aistar.entity.Pettype;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.UserLogin;
import tech.aistar.service.IPetService;
import tech.aistar.service.IUserInfoService;
import tech.aistar.service.IUserLoginService;
import tech.aistar.util.QiniuUtil;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/edit")
public class EditUserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IPetService petService;
    @Autowired
    private IUserLoginService userLoginService;
    @Autowired
    private QiniuUtil qiniuUtil;

    //用户修改成功(接收页面返回的)
    @PostMapping("/info")
    public String test(MultipartFile picfilename, Integer id, String uName,
                       String province, String city, String district, Integer uSex, java.util.Date uBirthday,
                       String uStatement){
        System.out.println("--------------------------------");
        //判断是添加还是修改

        Map<String,String> map = new HashMap<>();
        String imgUrl = null;
        try {
            imgUrl = qiniuUtil.upload(picfilename.getBytes(),picfilename.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgUrl", imgUrl);
        System.out.println(imgUrl);
        UserInfo userInfo = new UserInfo();
        userInfo.setPicfilename(map.get("imgUrl"));
        userInfo.setId(id);
        userInfo.setuName(uName);
        userInfo.setProvince(province);
        userInfo.setCity(city);
        userInfo.setDistrict(district);
        userInfo.setuSex(uSex);
        userInfo.setuBirthday(uBirthday);
        userInfo.setuStatement(uStatement);

        userInfoService.editUserInfo(userInfo.getuName(),new Timestamp(userInfo.getuBirthday().getTime()) ,userInfo.getuSex(),userInfo.getProvince(),userInfo.getCity(),userInfo.getDistrict(),userInfo.getuStatement(),userInfo.getPicfilename(),userInfo.getId());
        return "redirect:/info/details";
//        return "redirect:/info/data/"+0;   返回这个页面还停留在个人资料  但是无刷新
    }


    //修改数据处理层  宠物
    @GetMapping("/pet/{pid}")
    public String petTest(@PathVariable("pid")Long pid,Model model){
        System.out.println("--------pet-edit----------");
        Pet pet = petService.findById(pid);
        List<Pettype> plist = petService.findAll();
        model.addAttribute("plist",plist);
        model.addAttribute("pet",pet);
        return "/person/data";
    }


    //添加宠物
    @PostMapping("/addpet")
    public String addTest(Long pId, Integer id, String pName,
                          java.util.Date pBirthday,Integer pSex,Integer pType,
                          Integer pAge,MultipartFile picurl) {
        //判断是添加还是修改
        Map<String,String> map = new HashMap<>();
        String imgUrl = null;
        try {
            imgUrl = qiniuUtil.upload(picurl.getBytes(),picurl.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgUrl", imgUrl);
        System.out.println(imgUrl);

        Pet p = petService.findById(pId);
        Pet pet = new Pet();
        pet.setpId(pId);
        pet.setId(id);
        pet.setpName(pName);
        pet.setpBirthday(pBirthday);
        pet.setpSex(pSex);
        pet.setpType(pType);
        pet.setpAge(pAge);
        pet.setPicurl(map.get("imgUrl"));
        if (p == null) {
            petService.addPet(pet);
            //应该直接到宠物添加的页面
            return "redirect:/edit/pet/"+pet.getpId();
        } else {
            petService.editPet(pet.getpName(),pet.getpAge(),pet.getpSex(),0,pet.getPicurl(),new Date(pet.getpBirthday().getTime()),pet.getpType(),pet.getpId());
            //重定向到个人页
            return "redirect:/info/details";
        }
    }

    @PostMapping("/pass")
    public String testPass(UserLogin userLogin){
        System.out.println("进来了=================");
        userLoginService.editUserLogin(userLogin.getUserId(),userLogin.getUserPassword());
        return "redirect:/info/safety";
    }


}
